package com.sajus.web.billing.admin.action;

import java.util.List;

import javax.naming.NamingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sajus.billing.client.delegate.BusinessDelegateFactory;
import com.sajus.billing.client.dto.OfferingDTO;
import com.sajus.billing.client.dto.PromotionDTO;
import com.sajus.billing.client.dto.ServiceTypeDTO;

/**
 * Acts as a Struts 2 controller that responds
 * to a user action by setting the value
 * of the Message model class, and returns a String 
 * result.
 * @author Bruce Phillips
 *
 */
public class OfferingAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	/**
	 * The model class that stores the message
	 * to display in the view.
	 */
	private List<OfferingDTO> offerings;
	
	private OfferingDTO offering;
	
	/*
	 * Creates the MessageStore model object and 
	 * returns success.  The MessageStore model
	 * object will be available to the view.
	 * (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		if ( this.offering != null ) {
			if ( this.offering.getId() == null ){
				if ( this.offering.getDescription() != null ||
						this.offering.getServiceType() != null ){
					
//					BusinessDelegateFactory.getInstance().getServiceTypeManager().savePriceInfo( this.offering.getPriceInfo() );
					BusinessDelegateFactory.getInstance().getServiceTypeManager().saveOffering( this.offering );
					
				}
			} else {
				BusinessDelegateFactory.getInstance().getServiceTypeManager().saveOffering( this.offering );
				System.out.println("Persist object Success reset value" );
				this.offering = new OfferingDTO();
			}
		}
		
		String paramValue = ServletActionContext.getRequest().getParameter("offeringId");
		if ( paramValue != null && !paramValue.equals( "" ) ){
			Integer id = new Integer( Integer.parseInt( paramValue ) );
		
			this.offering = BusinessDelegateFactory.getInstance().getServiceTypeManager().getOffering( id );
		}
		if( this.offerings == null ){
			this.offerings = BusinessDelegateFactory.getInstance().getServiceTypeManager().getOfferings();
		}
		
		return SUCCESS;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public List<OfferingDTO> getOfferings() {
		return offerings;
	}


	public void setOfferings(List<OfferingDTO> offerings) {
		this.offerings = offerings;
	}


	public OfferingDTO getOffering() {
		return offering;
	}


	public void setOffering(OfferingDTO offering) {
		this.offering = offering;
	}
	
	public void setServiceType( Integer id ){
		
		System.out.println("Offering Service Type" + id.intValue() );
		
		if ( this.offering != null ){
			try {
				this.offering.setServiceType( BusinessDelegateFactory.getInstance()
						.getServiceTypeManager().getServiceType( id ) );
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}
	}
	
	public ServiceTypeDTO getServiceType(){
		if ( this.offering != null ){
			return this.offering.getServiceType();
		}
		return null;
	}
	
	public List<ServiceTypeDTO> getServiceTypes(){
		try {
			return BusinessDelegateFactory.getInstance().getServiceTypeManager().getServiceTypes();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}

}
